package com.own.demo.utils;


public class PrintNumbersinMatrixDiagonalPattern {

    public static void printPattern(int n) {
        // arr[][] 用于保存输出结果（对角线样式）
        int arr[][] = new int[n][n];
        int k, i, j, p = 1, f;

        for (k = 0; k < n; k++) {
            j = k;
            i = 0;
            while (j >= 0) {
                arr[i][j] = p;
                p++;
                i = i + 1;
                j = j - 1;
            }
        }


        for (k = 1; k < n; k++) {
            i = k;
            j = n - 1;
            f = k;
            while (j >= f) {
                arr[i][j] = p;
                p++;
                i = i + 1;
                j = j - 1;
            }
        }

        for (i = 0; i < n; i++) {
            for (j = 0; j < n; j++) {
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();
        }
    }
    public  static int[] findDiagonalOrder() {
        int[][] mat= new int[3][3];
        mat[0][0]=1;
        mat[0][1]=2;
        mat[0][2]=3;
        mat[1][0]=4;
        mat[1][1]=5;
        mat[1][2]=6;
        mat[2][0]=7;
        mat[2][1]=8;
        mat[2][2]=9;
        int  rows = mat.length;


        int  cols = mat[0].length;
        int[] res = new int[rows * cols];
        for(int i=0;i<cols * 2 - 1;i++){
           int   diff = cols - i - 1;
           for(int j=0;j<cols;j++){
              int  k = j - diff;
              if(isValidIndex(k, rows)){
                  System.out.print(mat[k][j]);
              }
           }
            System.out.println();
        }
        System.out.println("------------------------------------");
       for(int i=0;i<(rows+rows-1);i++){
           for (int j=0;j<i+1;j++){
              int  k = i-j;
               if (k<rows && k>=0 && j<rows){
                   System.out.print(mat[j][k]);
               }
           }
           System.out.println();
       }


        return res;
    }

    public static boolean  isValidIndex(int x, int n){
        return (x >= 0 && x < n);
    }

    public static void main(String[] args) {
        int[] d =findDiagonalOrder();


        for(int i=0;i<d.length;i++){
            System.out.print(d[i]+",");
        }
    }

}
